using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Typing_GUI
{
    public partial class frmResultOfLesson : Form
    {
        public frmResultOfLesson()
        {
            InitializeComponent();
        }
        public int error, score, speed, time;
        public string  lessonName;
        public int lessonID,userID;
        public int type = 1;
        private void frmResultOfLesson_Load(object sender, EventArgs e)
        {
            if (type == 1)
            {
                lblErrors.Text = error.ToString();
                lblScore.Text = score.ToString();
                lblSpeed.Text = speed + " (wpm)";
                lblTime.Text = (time / 60).ToString().PadLeft(2, '0') + ":" + (time % 60).ToString().PadLeft(2, '0'); ;
                lblLessonName.Text = lessonName;
            }
            else
            {
                lblErrors.Text = error.ToString();
                lblScore.Text = score.ToString();
                lblSpeed.Text = speed.ToString() + " (wpm) ";
                int mi=time/60;
                int se=time%60;
                string chuoi=mi.ToString().PadLeft(2,'0')+":"+se.ToString().PadLeft(2, '0');
                lblTime.Text =chuoi ;
                lblLessonName.Text = lessonName;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmResultOfLesson_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (type == 1)
            {
                CLASS.CBaiTap ls = CLASS.CBaiTap.GetLessonByID(lessonID);
                int temp = ls.CalculateScore();
                CLASS.CDiem point = new Typing_GUI.CLASS.CDiem();
                point.Mabaitap = lessonID;
                point.Manguoidung = userID;
                point.Sodiem = score - (error / temp);
                point.Soloisai = error;
                point.Thoigian = time;
                point.Tocdo = speed.ToString();
                if (!CLASS.CDiem.ExistMatch(userID, lessonID))
                    point.createMatch();
                else
                {
                    DialogResult result = MessageBox.Show("Your old score: " + CLASS.CDiem.getMatch(userID, lessonID).Sodiem + "\n Your new score:" + score.ToString() + " \n Do you want to change it?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (result == DialogResult.Yes)
                    {
                        point.updateMatch();
                    }
                }
            }
        }
    }
}